#include "myString.h"
#pragma setlocale( "" )



myString::myString(const char* s)
{	
	//setlocale(LC_ALL, ".936");
	if(s)
	{
		m_size = strlen(s) + 1;
		m_str = new char[m_size];
		char* m_str_cpy = m_str;
		while((*(m_str_cpy++) = *(s++)) != '\0'); 
	}
	else
	{
		m_size = 0;
		m_str = 0;
	}
};

myString::myString(const myString& str)
{
	m_size = str.m_size;
	m_str = new char[m_size];
	char* m_str_cpy = str.m_str;	
	strcpy(m_str, m_str_cpy);
	

};

myString::myString()
{
	m_size = NULL;
	m_str = 0;
};

myString::~myString()
{
	delete m_str;
};

int myString::length() const
{
	return m_size;
};

char myString::at(int n)
{
	assert(n >= 0 && n < m_size);
	return m_str[n-1];
};

char* myString::concat(const myString& str)
{
	int len = str.length() + m_size - 1;
	char* cat_str = new char[len];
	char* cat_str_cpy = cat_str;
	char* app_str = str.m_str;
	char* src_str = m_str;
	while((*(cat_str_cpy++) = *(src_str++))!='\0');
	cat_str_cpy--;
	while((*(cat_str_cpy++) = *(app_str++))!='\0');

	return cat_str;
};

int myString::find(const myString& subStr)
{
	if(subStr.length() > m_size) return -1;
	if(subStr.length() == 0) return 0;
	else
	{
		char* sub_str = subStr.m_str;
		char* src_str = m_str;
		int pos = 1;

		while(*src_str != '\0')
		{
			
			if(*src_str != *sub_str)
			{
				if(src_str == m_str || sub_str == subStr.m_str)
				{
					src_str++;
					pos++;

				}else
				{
					sub_str = subStr.m_str;				
				}
			}else
			{
				src_str++;
				sub_str++;
				pos++;
			}
			if(*sub_str == '\0') return pos - subStr.m_size + 1;
		}

		if(*src_str == '\0') return 0;
	}


};

char* myString::replace(const myString& str_src, const myString& str_rep)
{
	char* str1 = str_src.m_str;
	char* str1_beg = str_src.m_str;
	char* str2 = str_rep.m_str;
	char* str2_beg = str_rep.m_str;
	char* str_ful = m_str;
	char* str_new = new char[m_size * str_rep.m_size];
	char* str_new2 = str_new;
	int pos, offset, first;
	pos = offset = first = 0;
	while(*str_ful != '\0')
	{
		pos = myString(str_ful).find(str_src.m_str); 
		if(pos <= 0) 
		{
			if(str_ful == m_str) 
			{
				printf ("Cannot find the string %s!\n", str_src.m_str);
				return 0;
			}
			while((*(str_new++) = *(str_ful++))!='\0');
			break;

		}else
		{
			offset += pos + str_src.m_size - 2;

			for(int i = first; i < pos - 1; i++)
			{
				*(str_new++) = *(str1++);
			}

			str1 = str1_beg;

			while((*(str_new++) = *(str2++)) != '\0');			
			str2 = str2_beg;
			str_new--;

			first = offset;
			str_ful += offset;

		}
		
		
	}

	return str_new2;

};

myString& myString::operator=(const myString &other)
{
     if (this != &other)
     {
         int len = strlen(other.m_str);
         char *temp = new char[len+1];
         strcpy(temp,other.m_str);
         delete []m_str;
         m_str = temp;
		 m_size = other.m_size;
     }
     return *this;
}